package com.lbb.simple.demo.fragment

import android.graphics.Rect
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lbb.simple.databinding.FragmentFmBinding
import com.lbb.simple.demo.adapter.FmRecommendAdapter
import com.lbb.simple.demo.bean.Fm
import com.lbb.simple.demo.controller.FmController
import com.lbb.utils.DisplayUtil
import com.lbb.utils.recycler.BaseStateAdapter
import com.lbb.utils.recycler.LinearSpacingItemDecoration
import com.lbb.utils.recycler.RecyclerListener

class FmFragment : BaseFragment<FragmentFmBinding>(), FmController.FmView {
    private val fmPresenter = FmController.FmPresenter(this)
    private val fmRecommendAdapter = FmRecommendAdapter()

    init {
        fmRecommendAdapter.onClickErrorListener =
            RecyclerListener.OnClickErrorListener { fmPresenter.getFmRecommend() }
    }

    override fun getLayoutContent(): FragmentFmBinding {
        return FragmentFmBinding.inflate(layoutInflater)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        b.recyclerView.layoutManager = LinearLayoutManager(context)
        b.recyclerView.addItemDecoration(object : RecyclerView.ItemDecoration(){
            override fun getItemOffsets(
                outRect: Rect,
                view: View,
                parent: RecyclerView,
                state: RecyclerView.State
            ) {
                val position = parent.getChildAdapterPosition(view)
                val size = parent.adapter?.itemCount
                size?.let {
                    if(position!=0||position!=it-1){
                        outRect.bottom = DisplayUtil.dp2px(10f)
                        outRect.left = DisplayUtil.dp2px(10f)
                        outRect.right = DisplayUtil.dp2px(10f)
                    }
                }

            }
        })
        b.recyclerView.adapter = fmRecommendAdapter
    }

    override fun showLoadingFmRecommend() {
        fmRecommendAdapter.state = BaseStateAdapter.TYPE_LOADING
    }

    override fun showErrorFmRecommend() {
        fmRecommendAdapter.state = BaseStateAdapter.TYPE_ERROR
    }

    override fun setFmRecommend(list: List<Fm>) {
        fmRecommendAdapter.list = list as MutableList<Fm>

    }
}